﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows;
using System.Globalization;

namespace CodeStash.Addin.ValueConverters
{
    [ValueConversion(typeof(Boolean), typeof(Visibility))]
    public class BoolToVisibilityConverter : IValueConverter
    {
        private static Lazy<BoolToVisibilityConverter> instance = 
            new Lazy<BoolToVisibilityConverter>(() => new BoolToVisibilityConverter());

        private BoolToVisibilityConverter()
        {

        }

        static BoolToVisibilityConverter()
        {

        }


        public static BoolToVisibilityConverter Instance
        {
            get { return instance.Value; }
        }


        #region IValueConverter implementation


        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return Binding.DoNothing;

            if (parameter == null)
                return Binding.DoNothing;

            Boolean input = false;
            Boolean.TryParse(value.ToString(), out input);

            Boolean invertActive = false;
            Boolean.TryParse(parameter.ToString(), out invertActive);

            if (input)
            {
                return invertActive ? Visibility.Visible : Visibility.Hidden;
            }
            else
                return invertActive ? Visibility.Hidden : Visibility.Visible;

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException("Not implemented");
        }
        #endregion

    }
}
